﻿using SDATA;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SDLL
{
    public class DLL01
    {
        public static void Get_VoucherNo(ref IGB igb, string cnn, string gnVoucherTypeID, TextBox txtVoucherNo, string strTableName, string gnMonth, string gnYear, string gsDivisionID)
        {
            DataTable dt = new DataTable();
            string sql = "";        
            sql += "Select VoucherTypeID,Auto,S1,S2,S3,OutputLength,OutputOrder,Enabled,";
            sql += " Separated,Separator,Enabled1,Enabled2,Enabled3,S1Type,S2Type,S3Type";
            sql += " From tb0027";
            sql += " Where VoucherTypeID='" + gnVoucherTypeID + "' ";
            dt = DATA.GetDataTable(sql,cnn);
            if(dt.Rows.Count==1)
            {                
                if((dt.Rows[0]["Auto"]==null?"0":dt.Rows[0]["Auto"].ToString())=="1")
                {                           
                    igb.TableName = strTableName;
                    if((dt.Rows[0]["Enabled1"]==null?"0":dt.Rows[0]["Enabled1"].ToString()) == "1")
                        igb.StringKey1 = getType((dt.Rows[0]["S1Type"]==null?0:int.Parse(dt.Rows[0]["S1Type"].ToString())), (dt.Rows[0]["S1"]==null?"":dt.Rows[0]["S1"].ToString()), gnVoucherTypeID, gnMonth,gnYear,gsDivisionID);
                    else
                        igb.StringKey1 = "";
                    if ((dt.Rows[0]["Enabled2"] == null ? "0" : dt.Rows[0]["Enabled2"].ToString()) == "1")
                        igb.StringKey2 = getType((dt.Rows[0]["S2Type"] == null ? 0 : int.Parse(dt.Rows[0]["S2Type"].ToString())), (dt.Rows[0]["S2"] == null ? "" : dt.Rows[0]["S2"].ToString()), gnVoucherTypeID, gnMonth, gnYear, gsDivisionID);
                    else
                        igb.StringKey2 = "";
                    if ((dt.Rows[0]["Enabled3"] == null ? "0" : dt.Rows[0]["Enabled3"].ToString()) == "1")
                        igb.StringKey3 = getType((dt.Rows[0]["S3Type"] == null ? 0 : int.Parse(dt.Rows[0]["S3Type"].ToString())), (dt.Rows[0]["S3"] == null ? "" : dt.Rows[0]["S3"].ToString()), gnVoucherTypeID, gnMonth, gnYear, gsDivisionID);
                    else
                        igb.StringKey3 = "";                    
                    
                    if(dt.Rows[0]["OutputOrder"]!=null)
                    {
                        switch(int.Parse(dt.Rows[0]["OutputOrder"].ToString()))
                        {
                            case 0:
                                igb.OutputOrder = IGB.IGEOutputOrder.lmNSSS;
                                break;
                            case 1:
                                igb.OutputOrder = IGB.IGEOutputOrder.lmSNSS;
                                break;
                            case 2:
                                igb.OutputOrder = IGB.IGEOutputOrder.lmSSNS;
                                break;
                            case 3:
                                igb.OutputOrder = IGB.IGEOutputOrder.lmSSSN;
                                break;
                        }
                    }
                    if(dt.Rows[0]["OutputLength"]!=null)
                        igb.OutputLength = dt.Rows[0]["OutputLength"]==null?0:int.Parse(dt.Rows[0]["OutputLength"].ToString());

                    igb.SeperatorEnabled = dt.Rows[0]["Separated"]==null?false:(dt.Rows[0]["Separated"].ToString()=="0"?false:true);
                    
                    if((dt.Rows[0]["Separated"]==null?"0":dt.Rows[0]["Separated"].ToString())=="1")
                        igb.SeperatorCharacter = dt.Rows[0]["Separator"]==null?"":dt.Rows[0]["Separator"].ToString();
                    else
                        igb.SeperatorCharacter = "";
                    
                    if(igb.StringKey1.Length + igb.StringKey2.Length + igb.StringKey3.Length + (3 * igb.SeperatorCharacter.Length) >= igb.OutputLength)
                    {
                        MessageBox.Show("Số đã vượt quá mức quy định!\nBạn hãy thiết lập lại phiếu này.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtVoucherNo.Text = "";
                        return;
                    }
                    txtVoucherNo.Text = igb.Generate();
                }
                else
                    txtVoucherNo.Text = "";
            }
            else
            {
                txtVoucherNo.Text = "";
            }
        }

        private static string getType(int num, string s,  string  VoucherNo, string gnMonth, string gnYear, string gsDivisionID)
        {
            switch(num)
            {
                case 1:
                    return gnMonth.Replace(" ","0").PadLeft(2,'0');
                case 2:
                    return gnYear;
                case 3:
                    return VoucherNo;
                case 4:
                    return gsDivisionID;
                case 5:
                    return s;
                default:
                    return "";
            }
        } // .end

        public static bool GetID(string strTablesName, string strFieldName, string strValues, string strWhere, string cnn)
        {
            string strSQL = "";
            DataTable rs = new DataTable();
            strSQL = "SELECT 1 FROM " + strTablesName + " WHERE (" + strFieldName + " = '" + strValues + "')";
            if (strWhere.Trim() != "") { strSQL += " AND (" + strWhere + ")"; }
            rs = DATA.GetDataTable(strSQL,cnn);
            if (rs.Rows.Count > 0) { return true; }
            return false;
        } // .end

        public static string Get_VoucherID(string cnn, string tablename, string s1, string s2, string s3)
        {
            string s = "";
            bool TempKey = false;
            IGB igb = new IGB(cnn);
            igb.TableName = tablename;
            igb.StringKey1 = s1;
            igb.StringKey2 = s2;
            igb.StringKey3 = s3;
            igb.OutputOrder = IGB.IGEOutputOrder.lmSSSN;
            igb.OutputLength = 16;
            while(!TempKey)
            {                
              s = igb.Generate();
              igb.SaveNewKey();
              TempKey = !GetID(tablename, "VoucherID", s, "", cnn);
            }
            return s;
        }

        public static string Get_BatchID(string cnn, string tablename, string s1, string s2, string s3)
        {
            string s = "";
            bool TempKey = false;
            IGB igb = new IGB(cnn);
            igb.TableName = tablename;
            igb.StringKey1 = s1;
            igb.StringKey2 = s2;
            igb.StringKey3 = s3;
            igb.OutputOrder = IGB.IGEOutputOrder.lmSSSN;
            igb.OutputLength = 16;
            while (!TempKey)
            {
                s = igb.Generate();
                igb.SaveNewKey();
                TempKey = !GetID(tablename, "BatchID", s, "", cnn);
            }
            return s;
        }

        public static string Get_TransactionID(string cnn, string tablename, string s1, string s2, string s3)
        {
            string s = "";
            bool TempKey = false;
            IGB igb = new IGB(cnn);
            igb.TableName = tablename;
            igb.StringKey1 = s1;
            igb.StringKey2 = s2;
            igb.StringKey3 = s3;
            igb.OutputOrder = IGB.IGEOutputOrder.lmSSSN;
            igb.OutputLength = 16;
            while (!TempKey)
            {
                s = igb.Generate();
                igb.SaveNewKey();
                TempKey = !GetID(tablename, "TransactionID", s , "", cnn);
            }
            return s;
        }

    }
}

